//
// Created by zx208 on 2024/10/17.
//

// 删除单链表中所有值重复的结点，使得所有结点的值都不相同

// 思路：注意到题目中没有说单链表是否有序 所以我们直接采用双重循环进行判断

#include "../SingleLinkedList.h"

LinkedList Create_List_R(LinkedList head, int n){

    // 先创建头指针
    head = (LinkedList) malloc(sizeof( LNode));
    head->next = NULL;

    // 创建一个工作指针
    LinkedList node = head;

    for (int i = 0; i < n; ++i) {
        LinkedList p = (LinkedList) malloc(sizeof( LNode));
        int data;
        scanf(" %d", &data);
        p->data = data;
        p->next = node->next; // 即 p->next = NULL;
        node->next = p;
        node = p;
    }
    return head;
}

void PrintLL(LinkedList L){
    LNode *node = L->next;
    while(node){
        printf("%d ", node->data);
        node = node->next;
    }
}

Status Delete_Duplicate_LL(LinkedList L){
    LinkedList p = L->next, q, tmp;

    while(p){
        q = p->next, tmp = p;
        while(q){
            if(p->data == q->data){
                tmp->next = q->next;
                free(q);
                q = tmp->next;
            } else{
                tmp = q;
                q = q->next;
            }
        }
        p = p->next;
    }

    return OK;
}

int main(void){
    LinkedList L = NULL;
    L = Create_List_R(L, 5);
    Delete_Duplicate_LL(L);
    PrintLL(L);
    return 0;
}

// 1 1 2 2 3 3
